package com.qms.repast.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qms.repast.R;
import com.qms.repast.commons.GlideApp;
import com.qms.repast.entity.RecycleViewItemData;
import com.qms.repast.entity.resbean.ShopInfo.BodyBean.RowsBean;
import com.qms.repast.ui.activity.DiningActivity;
import com.qms.repast.utils.AppUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author 泅渡者
 * @date 2018/9/8 0008
 * @describe: 多布局可切换的适配器
 */
public class ReserveAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

	private List<RecycleViewItemData> dataList;
	private Context context;

	public ReserveAdapter (Context context) {
		this.context = context;
		this.dataList = new ArrayList<>();
	}

	@NonNull
	@Override
	public RecyclerView.ViewHolder onCreateViewHolder (@NonNull ViewGroup parent, int viewType) {
		LayoutInflater mInflater = LayoutInflater.from(context);
		RecyclerView.ViewHolder holder = null;

		View v = mInflater.inflate(R.layout.layout_reserve_item_normal, parent, false);
		holder = new SingleHolder(v);

		return holder;
	}

	@Override
	public void onBindViewHolder (@NonNull final RecyclerView.ViewHolder holder, final int position) {
		RowsBean rowsBean = (RowsBean) dataList.get(position).getT();
		if (holder instanceof SingleHolder) {
			initSingleData((SingleHolder) holder, rowsBean);
		} else if (holder instanceof DoubleHolder) {

		} else if (holder instanceof AdHolder) {

		}
	}

	@Override
	public int getItemViewType (int position) {
		RecycleViewItemData recycleViewItemData = dataList.get(position);
		return recycleViewItemData.getItemType();
	}

	@Override
	public int getItemCount () {
		return dataList == null ? 0 : dataList.size();
	}

	public void addList (List<RecycleViewItemData> shopList) {
		Iterator<RecycleViewItemData> iterator = shopList.iterator();
		while (iterator.hasNext()) {
			RecycleViewItemData recycleViewItemData = iterator.next();
			dataList.add(recycleViewItemData);
		}
		notifyDataSetChanged();
	}

	/**
	 * 填充ListView类型
	 *
	 * @param holder
	 * @param shopsBean
	 */
	private void initSingleData (SingleHolder holder, final RowsBean shopsBean) {
		GlideApp.with(context)
				.load(shopsBean.getLogo())
				.placeholder(R.mipmap.default_pic)
				.fitCenter()
				.into(holder.ivShopPic);
		holder.tvShopName.setText(shopsBean.getShopName());
		holder.tvAveragePrice.setText(shopsBean.getAvgMoney());
		holder.tvDistance.setText(AppUtils.distanceTransform(shopsBean.getDistance()));
		holder.tvAddress.setText(shopsBean.getAddress());

		holder.ivShopPic.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick (View view) {
				Intent intent = new Intent(context, DiningActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("shopId", shopsBean.getId() + "");
				intent.putExtra("shopName", shopsBean.getShopName());
				context.startActivity(intent);
			}
		});
	}

	/**
	 * 适配ListView类型
	 */
	class SingleHolder extends RecyclerView.ViewHolder {
		@BindView(R.id.iv_shopPic)
		ImageView ivShopPic;
		@BindView(R.id.tv_shopName)
		TextView tvShopName;
		@BindView(R.id.tv_address)
		TextView tvAddress;
		@BindView(R.id.tv_distance)
		TextView tvDistance;
		@BindView(R.id.tv_averagePrice)
		TextView tvAveragePrice;

		public SingleHolder (View itemView) {
			super(itemView);
			ButterKnife.bind(this, itemView);
		}
	}

	/**
	 * 适配GrideView类型
	 */
	class DoubleHolder extends RecyclerView.ViewHolder {

		public DoubleHolder (View itemView) {
			super(itemView);

		}
	}

	/**
	 * 广告类型，不做切换
	 */
	class AdHolder extends RecyclerView.ViewHolder {

		public AdHolder (View itemView) {
			super(itemView);

		}
	}
}
